// pages/center/center.js
import request from '../../utils/request'
// 手指在y轴上的初始值
let startY = 0
let moveY = 0
Page({

  data: {
    // 移动的范围
    convertTransform : '',
    convertTransition:'',
    userInfo: {},
    recentList:[]
  },

  onLoad(options) {
    const userInfo = wx.getStorageSync('userInfo_key')
    if(userInfo){
      this.setData({
        userInfo :JSON.parse(userInfo)
      })
      // console.log(this.data.userInfo);
      this.getRecentList(this.data.userInfo.userId)
    }
    // this.getRecentList(userInfo.userId)
  },
  handlerStart(event){
    this.setData({
      convertTransition:''
    })
    // 手指按下时的坐标
    startY = event.touches[0].clientY
  },
  handlerMove(event){
    // 手指移动的坐标
    const move = event.touches[0].clientY
    // 手指移动的距离
    moveY = move - startY

    // 控制边界值
    if(moveY<=0){
      moveY = 0
      return
    }
    if(moveY>60){
      moveY = 60
    }
    // 移动的动画
    this.setData({
      convertTransform: `translateY(${moveY}px)` ,
    })
  },
  handlerEnd(){
    this.setData({
      convertTransform: `translateY(0px)` ,
      convertTransition:`transform .8s linear`
    })
  },
  toLogin(){
    if(!wx.getStorageSync('userInfo_key')){
      wx.navigateTo({
        url: '/subpackages/login/login',
      })
    }
  },
  async getRecentList(uid){
    const re = await request('/user/record', { uid, type: 1 })
    // console.log(re);
    this.setData({
      recentList : re.weekData.splice(0,20).map(item=>{
        return {
          id:item.song.al.id,
          picUrl:item.song.al.picUrl
        }
      })
    })
  }
})